<?php
class mune extends CI_Controller {
	function index() {
		$this->pub->logp('浏览菜单','1-2');
		$da = $this -> pub -> get_setting('菜单', 'Mune');
		$this -> load -> view('adminx/munex', $da);
	}

	function add() {
		$this->pub->logp('创建菜单','1-2');
		$da['name'] = $this -> input -> post('name');
		$da['sort'] = $this -> input -> post('sort');
		$da['pid'] = $this -> input -> post('pid') ? $this -> input -> post('pid') : '0';
		$da['note'] = $this -> input -> post('note');
		$da['url'] = $this -> input -> post('url');
		$da['sf'] = $this -> input -> post('sf');

		$this -> pub -> insert('fl', $da);
		echo json_encode(array('type' => 1));
	}

	function up() {
		$this -> pub -> logp('栏目修改 - '.$this -> input -> post('idx'),'1-2');
		$da['name'] = $this -> input -> post('name');
		$da['sort'] = $this -> input -> post('sort');
		$da['note'] = $this -> input -> post('note');
		$da['url'] = $this -> input -> post('url');
		$da['sf'] = $this -> input -> post('sf');

		$this -> db -> update('fl', $da, array('fl_id' => $this -> input -> post('idx')));
		echo json_encode(array('type' => 1));
	}

	function get_one() {
		$this -> pub -> logp('获取栏目信息 - '.$this -> input -> post('idx'),'1-2');
		$q = $this -> db -> get_where('fl', array('fl_id' => $this -> input -> post('idx')));
		if ($q -> num_rows() > 0) {
			$da = $q -> row_array(0);
			$da['typex'] = 1;
			echo json_encode($da);
		} else {
			echo json_encode(array('typex' => 2));
		}
	}

	function del() {
		$this -> pub -> logp('栏目删除 - '.$this -> input -> post('idx'),'1-2');
		$q = $this -> db -> get_where('fl', array('fl_del' => 1, 'pid' => $this -> input -> post('idx')));
		if ($q -> num_rows() > 0) {
			$da['type'] = 2;
		} else {
			$da['type'] = 1;
			$this -> db -> update('fl', array('fl_del' => 2), array('fl_id' => $this -> input -> post('idx')));
		}
		echo json_encode($da);
	}

	function get() {
		$this -> pub -> logp('获取栏目信息 - ','1-2');
		$this -> db -> order_by('sort', 'asc');
		if ($this -> input -> post('key')) {
			$this -> db -> like('name', $this -> input -> post('key'));
		}
		if ($this -> input -> post('sf')) {
			$this -> db -> where('sf', $this -> input -> post('sf'));
		}
		$this -> db -> where('fl_del', 1);
		$q = $this -> db -> get_where('fl', array('pid' => '0'));
		$da['content'] = '';
		$sf['1'] = '关闭';
		$sf['2'] = '微信链接';
		$sf['3'] = '商品';
		$sf['4'] = '文章';
		$sf['5'] = '微信按钮';
		$sf['6'] = '机构分类';

		foreach ($q->result_array() as $v) {
			$da['content'] .= '<tr>';
			$da['content'] .= '<td>' . $v['fl_id'] . '</td>';
			$da['content'] .= '<td>' . $v['name'] . '</td>';
			$da['content'] .= '<td>' . $v['sort'] . '</td>';
			$da['content'] .= '<td>' . $sf[$v['sf']] . '</td>';
			$da['content'] .= '<td><button onclick="add_z(\'' . $v['fl_id'] . '\',\'' . $v['name'] . '\')" type="button" class="btn default"><i class="fa  fa-pencil"></i> 子栏目</button>  <button onclick="show_up(\'' . $v['fl_id'] . '\')" type="button" class="btn green"><i class="fa fa-edit"></i> 修改</button>  <button onclick="del(\'' . $v['fl_id'] . '\',\''.$vv['name'].'\')" type="button" class="btn red"><i class="fa fa-trash-o"></i> 删除</button></td>';
			$da['content'] .= '</tr>';
			$this -> db -> order_by('sort', 'asc');
			$this -> db -> where('fl_del', 1);
			$qq = $this -> db -> get_where('fl', array('pid' => $v['fl_id']));
			if ($qq -> num_rows() > 0) {
				foreach ($qq->result_array() as $vv) {
					$da['content'] .= '<tr><td>---------| ' . $vv['fl_id'] . '</td>';
					$da['content'] .= '<td>' . $vv['name'] . '</td>';
					$da['content'] .= '<td>' . $vv['sort'] . '</td>';
					$da['content'] .= '<td>' . $sf[$vv['sf']] . '</td>';
					$da['content'] .= '<td><button onclick="show_up(\'' . $vv['fl_id'] . '\')" type="button" class="btn green"><i class="fa fa-edit"></i> 修改</button>  <button onclick="del(\'' . $vv['fl_id'] . '\',\''.$vv['name'].'\')" type="button" class="btn red"><i class="fa fa-trash-o"></i> 删除</button></td>';
					$da['content'] .= '</tr>';
				}
			}
		}
		$da['type'] = 1;
		echo json_encode($da);
	}

}
?>